<?php

function member_dailymenu_page(){
        
        $header= array(
           '', 'Hétfő','Kedd','Szerda','Csütörtök','Péntek','Szombat','Vasárnap'
        );
        
        $query = db_select('ep_restaurants', 'r');
        
        $query->join('ep_offers', 'o', 'r.rid = o.rid');
        $query->fields('r',array('name','rid','city','website','telephone','open','delivery','wifi','parking'))
                ->fields('o',array('offer','price','oid','startdate','rid'));
               if (isset($_SESSION['varos']))$query 
                ->condition('city',$_SESSION['varos'],'=');

        $results = $query
			->execute();
	
        
        
        
        
        $restaurants = array();
        foreach($results->fetchAll() as $element){
            $offer = new stdClass();
            $offer->name=$element->offer;
            $offer->start = $element->startdate;
            
            $restaurants[$element->name][]=$offer;
                  
                  
            
            
            
            
            
        }
        
        
        foreach($restaurants as $restaurant_name => $offers){
            $rows[]=array(
              $restaurant_name,
              get_offer_by_day($offers, 0),
              get_offer_by_day($offers, 1),                
              get_offer_by_day($offers, 2),                
              get_offer_by_day($offers, 3),                
              get_offer_by_day($offers, 4),            
              get_offer_by_day($offers, 5),            
              get_offer_by_day($offers, 6)            
              );
         }
        $html = theme('table', 
			array(
				'header' => $header,
				'rows'=>$rows,		
				'empty' => t('Nincs elem ebben a listában...'),
			)
		);
        
        return '<table border="0"><tr><td>'.drupal_render(drupal_get_form('restaurant_get_restaurants_name_form')).'</td><td>'.drupal_render(drupal_get_form('member_get_offer_form')).'</td></tr></table>'.($html);
        
    }
    
function get_offer_by_day($offer_list,$day){

    foreach($offer_list as $offer){
        
        $start_date = strtotime($offer->start);
        $day_of_week= date('w',$start_date);
        if ($day_of_week==$day) 
            return $offer->name;
        
        
    }
}